package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.dao.BondConfigMapper;
import com.eastfair.venueservice.dto.BondConfigDTO;
import com.eastfair.venueservice.entity.BondConfig;
import com.eastfair.venueservice.service.BondConfigService;
import com.eastfair.venueservice.vo.BondConfigVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 保证金配置
 * </p>
 *
 * @author dqq
 * @date 2023-04-17
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class BondConfigServiceImpl extends SuperServiceImpl<BondConfigMapper, BondConfig> implements BondConfigService {

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<BondConfig> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(BondConfig model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<BondConfigVO> listBondConfigVO(BondConfigDTO query) {
        log.info("listBondConfigVO - 查询保证金配置VO列表，params={}", query);
        List<BondConfig> list = listBondConfig(query);
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        return list
                .stream()
                .map(bondConfig -> BeanUtil.toBean(bondConfig, BondConfigVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<BondConfig> listBondConfig(BondConfigDTO bondConfigDTO) {
        QueryWrapper<BondConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(BondConfig::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(bondConfigDTO.getExhibitionManageId() != null, BondConfig::getExhibitionManageId, bondConfigDTO.getExhibitionManageId())
                .eq(bondConfigDTO.getHomeId() != null, BondConfig::getHomeId, bondConfigDTO.getHomeId())
                .eq(StrUtil.isNotBlank(bondConfigDTO.getBelongSystem()), BondConfig::getBelongSystem, bondConfigDTO.getBelongSystem())
                .ge(bondConfigDTO.getSiteArea() != null, BondConfig::getMaxArea, bondConfigDTO.getSiteArea())
                .le(bondConfigDTO.getSiteArea() != null, BondConfig::getMinArea, bondConfigDTO.getSiteArea())
        ;
        return list(queryWrapper);
    }

    @Override
    public BondConfig getBySiteArea(Long exhibitionId, Long homeId, BigDecimal area) {
        log.info("getBySiteArea - 展会ID，主场ID，面积查询保证金配置, exhibitionId={}, homeId={}, area={}", exhibitionId, homeId, area);
        BondConfigDTO query = new BondConfigDTO();
        query.setExhibitionManageId(exhibitionId);
        query.setHomeId(homeId);
        query.setSiteArea(area);
        List<BondConfig> list = listBondConfig(query);
        if (list == null || list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBondConfig(BondConfigDTO bondConfigDTO) {
        log.info("saveBondConfig - 保存保证金配置列表, bondConfigDTO={}", bondConfigDTO);
        BondConfig bondConfig = BeanUtil.toBean(bondConfigDTO, BondConfig.class);
        return save(bondConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<BondConfigDTO> list) {
        log.info("saveBatch - 批量保存保证金配置, list={}", list);
        if (list == null || list.isEmpty()) {
            return false;
        }
        // 展会ID，主场ID删除保证金配置
        BondConfigDTO bondConfigDTO = list.get(0);
        removeByExhibitionIdAndHomeId(bondConfigDTO.getExhibitionManageId(), bondConfigDTO.getHomeId());
        // 批量保存保证金配置
        list.forEach(this::saveBondConfig);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByExhibitionIdAndHomeId(Long exhibitionId, Long homeId) {
        log.info("removeByExhibitionIdAndHomeId - 展会ID，主场ID删除保证金配置, exhibitionId={}, homeId={}", exhibitionId, homeId);
        BondConfigDTO query = new BondConfigDTO();
        query.setExhibitionManageId(exhibitionId);
        query.setHomeId(homeId);
        List<BondConfig> list = listBondConfig(query);
        return removeByIdsOfLogic(list);
    }
}
